<?php
header("Content-type: text/html; charset=gbk");
include_once "weixinUtil.php";
include_once "Constant.php";
//Needed model file
include_once $_SERVER['DOCUMENT_ROOT']."/model/User.php";
//Needed dao file
include_once $_SERVER['DOCUMENT_ROOT']."/dao/UserDAO.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/8/4
 * Time: 9:25
 */
$is_ajax = $_REQUEST['is_ajax'];
if(isset($is_ajax) && $is_ajax) {
    //传入参数
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];
    $userId = $_REQUEST['userId'];
    $phone = $_REQUEST['phone'];
    $userTag = $_REQUEST['userTag'];

    //构建user数组
    $user = array();
    $user["username"] = $username;
    $user["password"] = $password;
    $user["creditPublish"] = Constant::$INITIAL_PUBLISH_CREDIT;
    $user["creditWithdraw"] = Constant::$INITIAL_WITHDRAW_CREDIT;
    $user["headImg"] = "";
    $user["userTag"] = $userTag;
    $user["loginIndicator"] = Constant::$INITIAL_LOGIN_FLAG;
    $user["weixinId"] = $userId;
    $user["phone"] = $phone;

    //插入user对象
    $userDAO = new UserDAO();
    $result = $userDAO->insert($user);

    //注册成功，推送条消息通知
    if($result){
        //账户名可使用
        if($result == 1){
            $conn = new db();
            $db = $conn->getDBConn();
            $memcache = $conn->getMemcached();
            $tool = new weixinTool("HPQwjnca_An3LjptuP8zdQWvxkOANDn3FhLygAHLwe0");

            if($memcache->get('access_token')){
                $data = array();
                $data["content"] = "恭喜您成功注册成为CrowdFrame的用户，您可以开始浏览任务，接受任务并完成任务，快快开始吧！";
                $tool->send_message('text',1,$uesrId,$data);
                echo "success";
            }
        }
        //账户名已存在
        else
            echo "exist";

    }
    //注册失败
    else
        echo "fail".$result;
}
?>
